package com.kedacom.demo.emailgateway.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.kedacom.demo.emailgateway.model.Email;
import com.kedacom.demo.emailgateway.model.EmailException;
import com.kedacom.demo.emailgateway.model.EmailHistory;
import com.kedacom.demo.emailgateway.model.UnderwayEmail;

@Repository
public class EmailDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 发送成功后保存历史记录
	 * 
	 * @param emailHistory
	 */
	public void saveHistoryEmail(EmailHistory emailHistory) {
		String sql = "insert into email_history "
				+ "(title,text,create_date_time,from_email,to_email,send_date_time,company_id,remarks) "
				+ "values (?,?,?,?,?,?,?,?)";
		jdbcTemplate.update(
				sql,
				new Object[] { emailHistory.getSubject(), emailHistory.getText(), emailHistory.getCreateDateTime(),
						emailHistory.getFrom(), emailHistory.getTo(), emailHistory.getSendDateTime(),
						emailHistory.getCompanyMoid(), emailHistory.getRemarks() });
	}

	public void saveEmail(Email email) {
		String sql = "insert into email (title,text,create_date_time,from_email,to_email,num,company_id) "
				+ "values (?,?,?,?,?,?,?)";

		jdbcTemplate.update(
				sql,
				new Object[] { email.getSubject(), email.getText(), email.getCreateDateTime(), email.getFrom(),
						email.getTo(), 1, email.getCompanyMoid() });
	}

	/**
	 * 获取待发送列表
	 * 
	 * @return
	 */
	public List<UnderwayEmail> listEmails() {
		String sql = "select * from email limit 0,200";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		UnderwayEmail email = null;
		List<UnderwayEmail> listEmail = new ArrayList<UnderwayEmail>();
		for (Map<String, Object> m : list) {
			if (m != null) {
				email = new UnderwayEmail();
				email.setId(m.get("id").toString());
				email.setNum((Integer) m.get("num"));
				if (m.get("company_id") != null) {
					email.setCompanyMoid(String.valueOf(m.get("company_id")));
				}
				if (m.get("from_email") != null) {
					email.setFrom(String.valueOf(m.get("from_email")));
				}
				if (m.get("title") != null) {
					email.setSubject(String.valueOf(m.get("title")));
				}
				if (m.get("text") != null) {
					email.setText(String.valueOf(m.get("text")));
				}
				if (m.get("to_email") != null) {
					email.setTo(String.valueOf(m.get("to_email")));
				}
				if (m.get("create_date_time") != null) {
					email.setCreateDateTime(String.valueOf(m.get("create_date_time")));
				}
				listEmail.add(email);
			}
		}
		return listEmail;
	}

	/**
	 * 发送次数超过最大次数上限
	 * 
	 * 删除待发送邮件
	 * 
	 * @param underwayEmail
	 */
	public void deleteEmail(UnderwayEmail underwayEmail) {
		String sql = "delete from email where id =?";
		jdbcTemplate.update(sql, new Object[] { underwayEmail.getId() });
	}

	/**
	 * 更新发送次数
	 * 
	 * @param email
	 */
	public void updateTimes(UnderwayEmail email) {
		String sql = "update email set num=? where id =?";
		jdbcTemplate.update(sql, new Object[] { email.getNum(), email.getId() });
	}

	/**
	 * 发送次数超过最大次数 保存发送失败的邮件到异常表
	 * 
	 * @param email
	 */
	public void saveExceptionEmail(EmailException email) {
		String sql = "insert into email_exception  (title,text,create_date_time,from_email,to_email,send_date_time,exception,message,company_id) "
				+ "values (?,?,?,?,?,?,?,?,?)";
		jdbcTemplate.update(
				sql,
				new Object[] { email.getSubject(), email.getText(), email.getCreateDateTime(), email.getFrom(),
						email.getTo(), email.getSendDateTime(), email.getException(), email.getMessage(),
						email.getCompanyMoid() });
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}
